import { PlayerBase } from "./PlayerBase";
import { ClientConnection } from "../socket/connection/ClientConnection";

export abstract class PlayerBaseMgr<T extends PlayerBase> {

    players: Map<number, T> = new Map();

    /**
     * 设置玩家
     * @param player 
     */
    setPlayer(player: T) {
        this.players.set(player.userId, player);
    }

    /**
     * 将数据转换成userid
     * @param data 
     * @returns 
     */
    private convertToUserId(data: number | ClientConnection) {
        let userId = 0;
        if (data instanceof ClientConnection)
            userId = data.userId;
        else
            userId = data;
        return userId
    }

    /**
     * 判断有没有此玩家
     * @param data 
     * @returns 
     */
    hasPlayer(data: number | ClientConnection) {
        return this.players.has(this.convertToUserId(data));
    }

    /**
     * 移除玩家
     * @param data 
     */
    deletePlayer(data: number | ClientConnection) {
        this.players.delete(this.convertToUserId(data));
    }

    /**
     * 获取player
     * @param data 
     * @returns 
     */
    getPlayer(data: number | ClientConnection) {
        return this.players.get(this.convertToUserId(data));
    }
}